package com.simon.battery.chart;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.WindowManager;

import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.PointLabelFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import com.androidplot.xy.XYStepMode;
import com.simon.battery.dao.BatteryRepository;
import com.simon.battery.model.BatteryModel;
import com.simon.batterychecker.R;

public class BatteryXYPlotActivity extends Activity {
	private XYPlot plot;
	private BatteryRepository repos;
	private static final int ELEMENT_COUNT = 20;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// DB acces object
		repos = new BatteryRepository(this);

		// fun little snippet that prevents users from taking screenshots
		// on ICS+ devices :-)
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
				WindowManager.LayoutParams.FLAG_SECURE);

		setContentView(R.layout.simple_xy_plot_example);

		// initialize our XYPlot reference:
		plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);

		// Turn the above arrays into XYSeries':
		// XYSeries series1 = new SimpleXYSeries(getlevelList(), //
		// SimpleXYSeries
		// // takes a List
		// // so turn our
		// // array into a
		// // List
		// SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use
		// // the element index as
		// // the x value
		// ""); // Set the display title of the series
		Number[] numSightings = { 5, 8, 9, 2, 5 };
		Number[] years = { 2001, // 2001
				2002, // 2002
				2003, // 2003
				2004, // 2004
				2005 // 2005
		};

		 XYSeries series1 = new SimpleXYSeries(getPlotModel().getLevel(),
		 SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "");
		// XYSeries series1 = new SimpleXYSeries(Arrays.asList(years),
		// Arrays.asList(numSightings), "");
//		XYSeries series1 = new SimpleXYSeries(getPlotModel().getHour(),
//				getPlotModel().getLevel(), "");

		/**
		 * Create a formatter to use for drawing a series using
		 * LineAndPointRenderer and configure it from xml:
		 */
		LineAndPointFormatter series1Format = new LineAndPointFormatter();
		series1Format.setPointLabelFormatter(new PointLabelFormatter());
		series1Format.configure(getApplicationContext(),
				R.xml.line_point_formatter_with_plf1);

		// add a new series' to the xyplot:
		plot.addSeries(series1, new LineAndPointFormatter(Color.rgb(0, 0, 200),
				Color.rgb(0, 0, 100), null, (PointLabelFormatter) null));
		// reduce the number of range labels
		plot.setTicksPerRangeLabel(3);
		plot.getGraphWidget().setDomainLabelOrientation(-45);

	}

	private PlotModel getPlotModel() {
		List<BatteryModel> modelList;
		List<Integer> hours = new ArrayList<Integer>();
		List<Integer> levelList = new ArrayList<Integer>();
		List<Integer> hoursTmp = new ArrayList<Integer>();
		List<Integer> levelListTmp = new ArrayList<Integer>();

		modelList = repos.getAll();
		Calendar c = Calendar.getInstance();

		for (BatteryModel model : modelList) {
			levelListTmp.add(model.getLevel());
			c.setTime(new Date(Long.parseLong(model.getDate())));
			hoursTmp.add(c.get(Calendar.HOUR_OF_DAY));

			// System.out.println("TestSzSz " + model.getId() + ", "+
			// model.getDate() + ", " + model.getLevel());
		}

		if (levelListTmp.size() == hoursTmp.size()
				&& levelListTmp.size() >= ELEMENT_COUNT) {
			for (int i = levelListTmp.size() - 1 - ELEMENT_COUNT; i < levelListTmp.size(); i++) {
				levelList.add(levelListTmp.get(i));
				hours.add(hoursTmp.get(i));
			}
		}

		PlotModel plot = new PlotModel(levelList, hours);

		return plot;
	}

}

class PlotModel {
	private List<Integer> level = new ArrayList<Integer>();
	private List<Integer> hour = new ArrayList<Integer>();

	PlotModel() {
	}

	PlotModel(List<Integer> level, List<Integer> hour) {
		this.hour = hour;
		this.level = level;
	}

	public List<Integer> getLevel() {
		return level;
	}

	public void setLevel(List<Integer> level) {
		this.level = level;
	}

	public List<Integer> getHour() {
		return hour;
	}

	public void setHour(List<Integer> hour) {
		this.hour = hour;
	}

}
